'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 角色
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil');
var envUtil = require('../util/envUtil');
var _ = require('lodash');

/**
 * 角色
 */
var shopSchema = new mongoose.Schema({
    // id
    role_id: {
        type: Number,
        required: true,
        index: true
    },

    // 资源已购买商城列表
    resource_shop_list: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    },

    // 上次刷新时间
    last_refresh_time: {
        type: Date,
        default: Date.now
    }
});

/**
 * 增加资源商城已购买type
 * @return {[type]} [description]
 */
shopSchema.methods.addBuyResourceShopNum = function (shop_type) {
    this.resource_shop_list[shop_type] = this.resource_shop_list[shop_type] || 0;
    this.resource_shop_list[shop_type]++;
    this.markModified('resource_shop_list');
};

/**
 * 增加资源商城已购买type
 * @return {[type]} [description]
 */
shopSchema.methods.getBuyResourceShopList = function () {
    return this.resource_shop_list;
};

/**
 * 获取资源商城已购买type 次数
 * @return {[type]} [description]
 */
shopSchema.methods.getBuyResourceShopNum = function (shop_type) {
    return this.resource_shop_list[shop_type] || 0;
};

/**
 * 获取上次刷新时间
 * @return {[type]} [description]
 */
shopSchema.methods.getLastRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 刷新
 * @return {[type]} [description]
 */
shopSchema.methods.refresh = function () {
    this.resource_shop_list = {};
    this.last_refresh_time = Date.now();
};

if (!shopSchema.options.toJSON) {
    shopSchema.options.toJSON = {};
}
/* jshint unused:false */
shopSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Shop', shopSchema);
